package com.tencent.matrix;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.ArrayMap;

import androidx.annotation.NonNull;

import com.tencent.matrix.b.Matrix_B_B;
import com.tencent.matrix.g.Matrix_G_C;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public enum MatrixA {
    cvB("INSTANCE");
    private static MatrixA[] cvI = new MatrixA[]{cvB};

    private final Set<Matrix_B_B> cvC;
    public boolean cvD;
    public String cvE;
    a cvF;
    boolean cvG;
    public String cvH;
    Handler handler;

    final class a implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
        /*synthetic*/ MatrixA cvJ;
        boolean z;

        a(MatrixA matrixA, boolean b) {
            cvJ = matrixA;
            this.z = b;
        }

        public final void onActivityStarted(Activity activity) {
            cvE = activity.getClass().getName();
            if (!cvD && cvG) {
                Matrix_G_C.i("Matrix.AppActiveDelegate", "onForeground... visibleScene[%s]", cvE);
                handler.post(new Runnable() { // a$1
                    @Override
                    public void run() {

                    }
                });
            }
        }

        public final void onActivityStopped(@NonNull Activity activity) {
            if (GQ() == null) {
                if (!cvD && cvG) {
                    Matrix_G_C.i("Matrix.AppActiveDelegate", "onBackground... visibleScene[%s]", cvE);
                    handler.post(new Runnable() { // a$2
                        @Override
                        public void run() {

                        }
                    });
                }
            }
        }

        public final void onActivityCreated(@NonNull Activity activity, Bundle bundle) {
        }

        public final void onActivityDestroyed(@NonNull Activity activity) {
        }

        public final void onActivityResumed(@NonNull Activity activity) {
        }

        public final void onActivityPaused(@NonNull Activity activity) {
        }

        public final void onActivitySaveInstanceState(@NonNull Activity activity, Bundle bundle) {
        }

        public final void onConfigurationChanged(@NonNull Configuration configuration) {
        }

        public final void onLowMemory() {
        }

        public final void onTrimMemory(int i) {
            Matrix_G_C.i("Matrix.AppActiveDelegate", "[onTrimMemory] level:%s", i);
            if (i == 20 && MatrixA.this.cvD) {
                Matrix_G_C.i("Matrix.AppActiveDelegate", "onBackground... visibleScene[%s]", cvE);
                handler.post(new Runnable() { // a$2
                    @Override
                    public void run() {

                    }
                });
            }
        }
    }

    MatrixA(String str) {
        this.cvC = new HashSet<>();
        this.cvD = false;
        this.cvE = "default";
        this.cvF = new a(this, false);
        this.cvG = false;
    }

    public final void dB(String str) {
        Matrix_G_C.i("Matrix.AppActiveDelegate", "[setCurrentFragmentName] fragmentName:%s", str);
        this.cvH = str;
        StringBuilder stringBuilder = new StringBuilder();
        if (TextUtils.isEmpty(str)) {
            str = "?";
        }
        stringBuilder.append(str);
        this.cvE = stringBuilder.toString();
    }

    public final void a(Matrix_B_B bVar) {
        synchronized (this.cvC) {
            this.cvC.add(bVar);
        }
    }

    public final void b(Matrix_B_B bVar) {
        synchronized (this.cvC) {
            this.cvC.remove(bVar);
        }
    }

    public static String GQ() {
        long currentTimeMillis = System.currentTimeMillis();
        try {
            Map map;
            Class cls = Class.forName("android.app.ActivityThread");
            Object invoke = cls.getMethod("currentActivityThread", new Class[0]).invoke(null);
            Field declaredField = cls.getDeclaredField("mActivities");
            declaredField.setAccessible(true);
            if (Build.VERSION.SDK_INT < 19) {
                map = (HashMap) declaredField.get(invoke);
            } else {
                map = (ArrayMap) declaredField.get(invoke);
            }
            if (map.size() <= 0) {
                currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
                Matrix_G_C.d("Matrix.AppActiveDelegate", "[getTopActivityName] Cost:%s", currentTimeMillis);
                return null;
            }
            for (Object invoke2 : map.values()) {
                Class cls2 = invoke2.getClass();
                Field declaredField2 = cls2.getDeclaredField("paused");
                declaredField2.setAccessible(true);
                if (!declaredField2.getBoolean(invoke2)) {
                    declaredField = cls2.getDeclaredField("activity");
                    declaredField.setAccessible(true);
                    String name = ((Activity) declaredField.get(invoke2)).getClass().getName();
                    currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
                    Matrix_G_C.d("Matrix.AppActiveDelegate", "[getTopActivityName] Cost:%s", currentTimeMillis);
                    return name;
                }
            }
            currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
            Matrix_G_C.d("Matrix.AppActiveDelegate", "[getTopActivityName] Cost:%s", currentTimeMillis);
        } catch (Throwable e) {
            currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
            Matrix_G_C.d("Matrix.AppActiveDelegate", "[getTopActivityName] Cost:%s", currentTimeMillis);
        }
        return null;
    }
}
